﻿using System;
using System.ComponentModel;
using System.IO;
using ThePresenter.Net.PresenterCom.Uids;


namespace ThePresenter.Polls.Data
{
	[Serializable]
	public class AnswerData : INotifyPropertyChanged
	{
		#region Fields
		private String _Author;

		public Uid AnswerSource;
		#endregion


		#region Properties
		/// <summary>
		/// Gets or sets the author.
		/// </summary>
		/// <value>The author.</value>
		public String Author
		{
			get
			{
				return _Author;
			}
			set
			{
				if (_Author != value)
				{
					_Author = value;
					OnPropertyChanged("Author");
				}
			}
		}
		#endregion


		#region Methods
		/// <summary>
		/// Serializes this instance.
		/// </summary>
		/// <returns>Serialized instance.</returns>
		public byte[] Serialize()
		{
			byte[] bytes = null;

			using (var ms = new MemoryStream())
			{
				Poll.Formatter.Serialize(ms, this);
				bytes = ms.ToArray();
			}

			return bytes;
		}
		#endregion


		#region INotifyPropertyChanged Members

		/// <summary>
		/// Occurs when a property value changes.
		/// </summary>
		[field: NonSerialized]
		public event PropertyChangedEventHandler PropertyChanged;

		/// <summary>
		/// Called when property changed.
		/// </summary>
		/// <param name="propertyName">Name of the property.</param>
		protected void OnPropertyChanged(string propertyName)
		{
			if (PropertyChanged != null)
				PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
		}

		#endregion
	}
}
